package com.yuanfeng.activity.service.impl;

import com.yuanfeng.activity.mapper.ActivityDiscountBaseMapper;
import com.yuanfeng.activity.mapper.ActivityDiscountGoodsMapper;
import com.yuanfeng.activity.mapper.ActivitySeckillGoodsMapper;
import com.yuanfeng.activity.service.ActivityGoodsService;
import com.yuanfeng.activity.vo.*;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

/**
 * @ClassName ActivityGoodsServiceImpl
 * @Description
 * @Author cyp
 * @Date 2023-12-06 14:13:09
 * @Version 1.0
 **/
@Service
public class ActivityGoodsServiceImpl implements ActivityGoodsService {

    @Autowired
    private ActivityDiscountGoodsMapper activityDiscountGoodsMapper;

    @Autowired
    private ActivityDiscountBaseMapper activityDiscountBaseMapper;

    @Autowired
    private ActivitySeckillGoodsMapper activitySeckillGoodsMapper;

    @Override
    public ResponseResult checkGoodsLimit(Map<String, Object> param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if (null != userInfo) {
            String userId = userInfo.getUserId();
            List<Map<String,Object>> list = (List) param.get("list");
            List<Integer> commonIds = new ArrayList<>();
            List<Map<String,Object>> commonMapList = new ArrayList<>();
            for (Map<String, Object> map : list) {
                Integer commonId = (Integer) map.get("commonId");
                if (!commonIds.contains(commonId)){
                    commonIds.add(commonId);
                    commonMapList.add(map);
                }else{
                    for (Map<String, Object> stringObjectMap : commonMapList) {
                        if(stringObjectMap.get("commonId").equals(commonId)){
                            Integer goodsNum = (Integer) stringObjectMap.get("goodsNum") + (Integer) map.get("goodsNum");
                            stringObjectMap.put("goodsNum",goodsNum);
                        }
                    }
                }
            }
            Integer flag = 0;
            for (Map<String, Object> stringObjectMap : commonMapList) {
                Integer commonId = (Integer) stringObjectMap.get("commonId");
                Integer goodsNum = (Integer) stringObjectMap.get("goodsNum");
                //限时折扣
                CheckDiscountLimitVO data = activityDiscountGoodsMapper.queryDiscountInfo(commonId);
                Integer count = null;
                if (null != data) {
                    Integer goodsUpperLimit = data.getGoodsUpperLimit();
                    Integer discountUpperTime = data.getDiscountUpperTime();
                    if (0 == goodsUpperLimit && 0 == discountUpperTime) {
                        flag = 1;
                        continue;
                    }
                    Calendar cal = Calendar.getInstance();
                    Date fdate = new Date();
                    cal.setTime(fdate);
                    cal.add(Calendar.DATE, -discountUpperTime);
                    fdate = cal.getTime();
                    // 获取当天的日期
                    LocalDate today = LocalDate.now();
                    // 设置当天的开始时间为00:00:00
                    LocalDateTime startOfDay = today.atStartOfDay();
                    // 设置当天的结束时间为23:59:59.999999999
                    LocalDateTime endOfDay = LocalDateTime.of(today, LocalTime.MAX);
                    param.put("fDate", fdate);
                    param.put("commonId", commonId);
                    param.put("userId", userId);
                    param.put("goodsPrice", data.getDiscountPrice());
                    param.put("goodsStartTime", startOfDay);
                    param.put("goodsEndTime", endOfDay);
                    count = activityDiscountBaseMapper.queryOrderGoodsCount(param);
                    count = count == null ? 0 : count;
                    if ((count + goodsNum) > goodsUpperLimit) {
                        return ResponseResult.fail(2,"商品【"+data.getGoodsName()+"】已达购买上限");
                    }
                }

                //秒杀
                CheckSeckillLimitVO data2 = activitySeckillGoodsMapper.querySeckillInfo(param);
                Integer count2 = null;
                if(null != data2){
                    Integer goodsUpperLimit = data2.getGoodsUpperLimit();
                    if (0 == goodsUpperLimit) {
                        flag = 1;
                        continue;
                    }
                    param.put("goodsStartTime",data2.getGoodsStartTime());
                    param.put("goodsEndTime",data2.getGoodsEndTime());
                    param.put("commonId", commonId);
                    param.put("userId", userId);
                    if(null != userId){
                        count2 = activitySeckillGoodsMapper.queryOrderGoodsCount(param);
                    }
                    count2 = count2==null?0:count2;
                    data2.setBuyNum(count2);
                    if((count2 + goodsNum) >= goodsUpperLimit){
                        return ResponseResult.fail(2,"商品【"+data2.getGoodsName()+"】已达购买上限");
                    }
                }
            }
            if(flag == 1){
                return ResponseResult.success();
            }
            return ResponseResult.success();
        }else{
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);

        }
    }

}

